package com.qixin.modules.eaApplication.service.impl;

import com.qixin.modules.ea.entity.EaEntertainReceptionActivity;
import com.qixin.modules.ea.entity.EaPhoneRecord;
import com.qixin.modules.ea.mapper.EaEntertainReceptionActivityMapper;
import com.qixin.modules.ea.mapper.EaPhoneRecordMapper;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.entity.EaHostObject;
import com.qixin.modules.eaApplication.mapper.EaHostObjectMapper;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: ea_entertain_apply
 * @Author: qixin-boot
 * @Date:   2022-01-21
 * @Version: V1.0
 */
@Service
public class EaEntertainApplyServiceImpl extends ServiceImpl<EaEntertainApplyMapper, EaEntertainApply> implements IEaEntertainApplyService {

	@Autowired
	private EaEntertainApplyMapper eaEntertainApplyMapper;
	@Autowired
	private EaHostObjectMapper eaHostObjectMapper;
	@Autowired
	private EaPhoneRecordMapper eaPhoneRecordMapper;
	@Autowired
	private EaEntertainReceptionActivityMapper eaEntertainReceptionActivityMapper;
	
	@Override
	@Transactional
	public void saveMain(EaEntertainApply eaEntertainApply, List<EaHostObject> eaHostObjectList, List<EaEntertainReceptionActivity> eaEntertainReceptionActivityList , EaPhoneRecord eaPhoneRecord) {
		eaEntertainApplyMapper.insert(eaEntertainApply);
		if(eaHostObjectList!=null && eaHostObjectList.size()>0) {
			for(EaHostObject entity:eaHostObjectList) {
				//外键设置
				entity.setEntertainApplyId(eaEntertainApply.getId());
				eaHostObjectMapper.insert(entity);
			}
		}
		if(eaPhoneRecord!=null) {
			//外键设置
			eaPhoneRecord.setEntertainApplyId(eaEntertainApply.getId());
			eaPhoneRecordMapper.insert(eaPhoneRecord);

		}
		if(eaEntertainReceptionActivityList!=null && eaEntertainReceptionActivityList.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivityList) {
				//外键设置
				entity.setPid(eaEntertainApply.getId());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(EaEntertainApply eaEntertainApply,List<EaHostObject> eaHostObjectList,List<EaEntertainReceptionActivity> eaEntertainReceptionActivityList,EaPhoneRecord eaPhoneRecord) {
		eaEntertainApplyMapper.updateById(eaEntertainApply);
		
		//1.先删除子表数据
		eaHostObjectMapper.deleteByMainId(eaEntertainApply.getId());
		eaPhoneRecordMapper.deleteByMainId(eaEntertainApply.getId());
		eaEntertainReceptionActivityMapper.deleteByMainId(eaEntertainApply.getId());
		
		//2.子表数据重新插入
		if(eaHostObjectList!=null && eaHostObjectList.size()>0) {
			for(EaHostObject entity:eaHostObjectList) {
				//外键设置
				entity.setEntertainApplyId(eaEntertainApply.getId());
				eaHostObjectMapper.insert(entity);
			}
		}
		if(eaPhoneRecord!=null ) {
			//外键设置
			eaPhoneRecord.setEntertainApplyId(eaEntertainApply.getId());
			eaPhoneRecordMapper.insert(eaPhoneRecord);
		}
		if(eaEntertainReceptionActivityList!=null && eaEntertainReceptionActivityList.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivityList) {
				//外键设置
				entity.setPid(eaEntertainApply.getId());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		eaHostObjectMapper.deleteByMainId(id);
		eaPhoneRecordMapper.deleteByMainId(id);
		eaEntertainReceptionActivityMapper.deleteByMainId(id);
		eaEntertainApplyMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaHostObjectMapper.deleteByMainId(id.toString());
			eaPhoneRecordMapper.deleteByMainId(id.toString());
			eaEntertainReceptionActivityMapper.deleteByMainId(id.toString());
			eaEntertainApplyMapper.deleteById(id);
		}
	}
	
}
